package com.nand.module.function.adapter;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.text.Editable;
import android.text.Html;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.animation.ScaleInAnimation;
import com.nand.module.function.R;
import com.nand.module.function.bean.EqDatailBean;
import com.nand.module.function.utils.ToolUtils;

import org.xml.sax.XMLReader;

import java.util.ArrayList;
import java.util.List;

public class EquipmentAdapter extends RecyclerView.Adapter<EquipmentAdapter.ViewHolder> implements View.OnClickListener {
    private static final int EQUIPMENT_LEDGER_CODE = 0; //设备台账
    private static final int EQUIPMENT_INSPECTION_CODE = 1; //设备巡检
    private static final int EQUIPMENT_REPAIR_CODE = 2; //设备报修
    private static final int UPKEEP_PAGE_CODE = 3; //保养界面
    private Context mContext;
    private int type;
      private List<EqDatailBean.DataListBean> mList = new ArrayList<>();

    private OnItemClickListener mOnItemClickListener = null;
    private RequestOptions error;

    public EquipmentAdapter(Context mContext, int type) {
        this.mContext = mContext;
        this.type = type;
    }

    public void setData(List<EqDatailBean.DataListBean> data) {
        this.mList = data;
        notifyDataSetChanged();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View layout = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_eq_list, viewGroup, false);
        layout.setOnClickListener(this);
        return new ViewHolder(layout);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        viewHolder.itemView.setTag(position);

        error = new RequestOptions().placeholder(R.mipmap.item_bg).error(R.mipmap.item_bg);

        EqDatailBean.DataListBean bean = mList.get(position);
        String filePath = bean.getFilePath();
        if ("".equals(filePath) || null == filePath){
            viewHolder.mIvIcon.setVisibility(View.INVISIBLE);
        }else {
            Glide.with(mContext).asBitmap().load(filePath).apply(error).into(viewHolder.mIvIcon);
        }

        switch (type) {
            case EQUIPMENT_LEDGER_CODE: //设备台账

                viewHolder.mTextview3.setText(Html.fromHtml("设备归属:&#12288;<font color='#696969'>"+bean.getEquipBelong()+"</font>"));
                viewHolder.mTextview8.setText(bean.getEquipState());
                viewHolder.mTextview1.setText( Html.fromHtml("设备编号:&#12288;<font color='#696969'>"+bean.getEquipID()+"</font>"));
                viewHolder.mTextview2.setText( Html.fromHtml("设备名称:&#12288;<font color='#696969'>"+bean.getEquipName()+"</font>"));
                viewHolder.mTextview4.setText( Html.fromHtml("创建时间:&#12288;<font color='#696969'>"+bean.getCreatedTime()+"</font>"));
                viewHolder.mTextview5.setVisibility(View.GONE);
                viewHolder.mTextview6.setVisibility(View.GONE);
                viewHolder.mTextview7.setVisibility(View.GONE);
                break;
            case EQUIPMENT_INSPECTION_CODE: //设备巡检
                viewHolder.mTextview1.setText(Html.fromHtml("单据编号:&#12288;<font color='#696969'>"+bean.getCheckID()+"</font>"));
                viewHolder.mTextview2.setText(Html.fromHtml("设备编号:&#12288;<font color='#696969'>"+bean.getEquipID()+"</font>"));
                viewHolder.mTextview3.setText(Html.fromHtml("设备名称:&#12288;<font color='#696969'>"+bean.getEquipName()+"</font>"));
                int isPobigint = bean.getIsPobigint();
                if (0 == isPobigint ){
                    viewHolder.mTextview4.setText(Html.fromHtml("点 / 巡检:&#12288;<font color='#696969'>点检</font>"));
                }else {
                    viewHolder.mTextview4.setText(Html.fromHtml("点 / 巡检:&#12288;<font color='#696969'>巡检</font>"));
                }
                int checkResult = bean.getCheckResult();
                switch (checkResult){
                    case 0:
                        viewHolder.mTextview8.setText("不合格");
                        break;
                    case 1:
                        viewHolder.mTextview8.setText("合格");
                        break;
                    case 2:
                        viewHolder.mTextview8.setText("停机");
                        break;
                }
                viewHolder.mTextview5.setText(Html.fromHtml("创建时间:&#12288;<font color='#696969'>"+bean.getCreatedTime()+"</font>"));
                viewHolder.mTextview6.setVisibility(View.GONE);
                viewHolder.mTextview7.setVisibility(View.GONE);
                break;
            case EQUIPMENT_REPAIR_CODE: //设备报修
                viewHolder.mTextview1.setText(Html.fromHtml("单据编号:&#12288;<font color='#696969'>"+bean.getRepairId()+"</font>"));
                String state = bean.getState();
                if ("0".equals(state)){
                    viewHolder.mTextview8.setText("未生效");
                }else {
                    viewHolder.mTextview8.setText("已生效");
                }

                viewHolder.mTextview2.setText(Html.fromHtml("设备编号:&#12288;<font color='#696969'>"+bean.getEquipID()+"</font>"));
                viewHolder.mTextview3.setText(Html.fromHtml("设备名称:&#12288;<font color='#696969'>"+bean.getEquipName()+"</font>"));
                viewHolder.mTextview4.setText(Html.fromHtml("故障等级:&#12288;<font color='#696969'>"+bean.getHitchGraContent()+"</font>"));
                viewHolder.mTextview5.setText(Html.fromHtml("故障类别:&#12288;<font color='#696969'>"+bean.getHitchType()+"</font>"));
                viewHolder.mTextview6.setText(Html.fromHtml("创建时间:&#12288;<font color='#696969'>"+bean.getCreatedTime()+"</font>"));
                viewHolder.mTextview7.setVisibility(View.GONE);

                break;
            case UPKEEP_PAGE_CODE: //保养界面
                viewHolder.mTextview1.setText(Html.fromHtml("单据编号:&#12288;<font color='#696969'>"+bean.getMaintenanceId()+"</font>"));
                int maintenanceState = bean.getMaintenanceState();
                switch (maintenanceState){
                    case 0:
                        viewHolder.mTextview8.setText("待提交");
                        break;
                    case 1:
                        viewHolder.mTextview8.setText("待计划审核");
                        break;
                    case 2:
                        viewHolder.mTextview8.setText("待生产审核");
                        break;
                    case 3:
                        viewHolder.mTextview8.setText("待执行");
                        break;
                    case 4:
                        viewHolder.mTextview8.setText("保养完成");
                        break;

                }

                viewHolder.mTextview2.setText(Html.fromHtml("设备编号:&#12288;<font color='#696969'>"+bean.getEquipID()+"</font>"));
                viewHolder.mTextview3.setText(Html.fromHtml("设备名称:&#12288;<font color='#696969'>"+bean.getEquipName()+"</font>"));

                String planStartDate = bean.getPlanStartDate();
                String substring = planStartDate.substring(0, planStartDate.length() - 3);
                String planEndDate = bean.getPlanEndDate();
                String substring2 = planEndDate.substring(0, planEndDate.length() - 3);
                viewHolder.mTextview5.setText(Html.fromHtml("计划时间:&#12288;<font color='#F6BD32'><size>"+substring+"</size></font><font color='#696969'>&#8197;至&#8197;</font><font color='#F6BD32'><size>"+substring2+"</size></font>",null,new ToolUtils.SizeLabel(13,mContext)));
                //viewHolder.mTextview5.setText(Html.fromHtml("计划结束时间:&#12288;<font color='#696969'>"+substring2+"</font>")"计划结束时间："+substring2);
                viewHolder.mTextview4.setText(Html.fromHtml("创建时间:&#12288;<font color='#696969'>"+bean.getCreatedTime()+"</font>"));
                viewHolder.mTextview6.setVisibility(View.GONE);
                viewHolder.mTextview7.setVisibility(View.GONE);
                break;
            default:
                break;
        }

    }


    @Override
    public int getItemCount() {
        if (mList != null)
            return mList.size();
        return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView mTextview1, mTextview2, mTextview3, mTextview4, mTextview5,mTextview6,mTextview7,mTextview8;
        ImageView mIvIcon;

        public ViewHolder(View itemView) {
            super(itemView);
            mTextview1 = itemView.findViewById(R.id.textview1);
            mTextview2 = itemView.findViewById(R.id.textview2);
            mTextview3 = itemView.findViewById(R.id.textview3);
            mTextview4 = itemView.findViewById(R.id.textview4);
            mTextview5 = itemView.findViewById(R.id.textview5);
            mTextview6 = itemView.findViewById(R.id.textview6);
            mTextview7 = itemView.findViewById(R.id.textview7);
            mTextview8 = itemView.findViewById(R.id.textview8);
            mIvIcon = itemView.findViewById(R.id.iv_icon);


        }

    }
    //两次点击按钮之间的间隔，目前为1000ms
    private static final int MIN_CLICK_DELAY_TIME = 1000;
    private long lastClickTime;
    public static interface OnItemClickListener {

        void onItemClick(View view, int position);

    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            long curClickTime = System.currentTimeMillis();
            if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
                lastClickTime = curClickTime;
                //注意这里使用getTag方法获取position
                mOnItemClickListener.onItemClick(v, (int) v.getTag());
            }
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }



}

